﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Ext.Net;
using Ext.Net.MVC;
using WebEntities;
using WebEntities.Models.WebInfo;
using com.subb.WebInfo;

namespace WebAdmin.Controllers
{
    [Authorize]
    public class AdvSuiteController : Controller
    {
        public ActionResult GetAllAdvSuite(bool? withAllOption)
        {
            WI_AdvSuiteDAL asdal = new WI_AdvSuiteDAL();
            List<WI_AdvSuite> wiSuiteList = asdal.GetAllAdvSuite();
            List<AdvSuite> suiteList = new List<AdvSuite>();
            if (withAllOption ?? false)
            {
                suiteList.Add(new AdvSuite { SuiteName = "全部", SuiteID = 0 });
            }
            foreach (var wiSuite in wiSuiteList)
            {
                AdvSuite adSuite = new AdvSuite
                {
                    SuiteID = wiSuite.SuiteID,
                    SuiteName = wiSuite.SuiteName
                };
                suiteList.Add(adSuite);
            }
            return this.Store(suiteList);
        }

        [HttpPost]
        public ActionResult DeleteAdvSuite(List<AdvSuite> delSuite)
        {
            try
            {
                WI_AdvSuiteDAL asdal = new WI_AdvSuiteDAL();
                foreach (var suite in delSuite)
                {
                    asdal.RemoveAdvSuite(suite.SuiteID);
                }
                return this.Direct(new { Success = true, Msg = "删除成功！" });
            }
            catch (Exception ex)
            {
                return this.Direct(new { Success = false, Msg = ex.InnerException != null ? ex.InnerException.Message : ex.Message });
            }
        }

        public ActionResult NewAdvSuiteWindow()
        {
            Window wnd = new Window()
            {
                ID = "Window1",
                Title = "新建广告栏目",
                Width = 500,
                Height = 150,
                Modal = true,
                Maximizable = true,
                Icon = Ext.Net.Icon.TextListBullets,
                Loader = new ComponentLoader
                {
                    Url = Url.Action("CreateAdvSuite"),
                    Mode = LoadMode.Frame,
                    LoadMask =
                    {
                        ShowMask = true,
                        Msg = "加载中，请稍候..."
                    }
                }
            };
            wnd.Render();
            return this.Direct();
        }

        public ActionResult CreateAdvSuite()
        {
            return View();
        }

        [HttpPost]
        public ActionResult CreateAdvSuite(string SuiteName)
        {
            FormPanelResult response = new FormPanelResult();
            try
            {
                WI_AdvSuiteDAL asdal = new WI_AdvSuiteDAL();
                asdal.CreateAdvSuite(SuiteName);
                response.Success = true;
                response.ExtraParams.Add(new Parameter("Msg", "栏目创建成功！"));
                return response;
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.ExtraParams.Add(new Parameter("Msg", ex.InnerException != null ? ex.InnerException.Message : ex.Message));
                return response;
            }
        }

        [HttpPost]
        public ActionResult UpdateAdvSuite(List<AdvSuite> updSuite)
        {
            FormPanelResult response = new FormPanelResult();
            try
            {
                WI_AdvSuiteDAL asdal = new WI_AdvSuiteDAL();
                foreach (var suite in updSuite)
                {
                    asdal.UpdateAdvSuite(suite.SuiteID, suite.SuiteName);
                }
                response.Success = true;
                response.ExtraParams.Add(new Parameter("Msg", "栏目更新成功！"));
                return response;
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.ExtraParams.Add(new Parameter("Msg", ex.InnerException != null ? ex.InnerException.Message : ex.Message));
                return response;
            }
        }
    }
}
